﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.Utils.ErrorProvider;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.DataAccess.Entities;
using System.Data;
namespace Churchtoolkit.Web
{
    public partial class EducationWorkView : Page<EducationWorkPresenters>, IEducationWorkView
    {
        #region [private Fileds]
        private Label lbl;
        private MemberEducation CurrentMemberEducation;
        private MemberJobs CurrentMemberJob;

        #endregion

        #region [ Events ]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                this.vldreqtxtCollageUniversity.Replace("[Field]", "Collage/University");
                this.vldreqtxtEmployeer.Replace("[Field]", "Employeer");
                this.vldreqtxtPosition.Replace("[Field]", "Position");
                this.FillMemberEducationlist();
                this.FillMemberJobsList();
                this.Fillddlyear();
            }
            this.Page.MaintainScrollPositionOnPostBack = true;
        }

        #region [Education]
        protected void btnSaveEducation_Click(object sender, EventArgs e)
        {
            SaveEducationInformation();
            FillMemberEducationlist();
        }
        protected void btnRemoveEducation_Click(object sender, EventArgs e)
        {
            RemoveEducationInformation();
        }
        protected void dlstEducationInformation_ItemCommand(object source, DataListCommandEventArgs e)
        {
            ButtonAction(e.CommandName, e.CommandArgument);
        }
        protected void dlstEducationInformation_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                CurrentMemberEducation = (MemberEducation)e.Item.DataItem;
                if (CurrentMemberEducation.Type == 0)
                {
                    lbl = (Label)e.Item.FindControl("lblType");
                    lbl.Text = "Collage";
                }
                if (CurrentMemberEducation.Type == 1)
                {
                    lbl = (Label)e.Item.FindControl("lblType");
                    lbl.Text = "University";
                }
            }
        }
        protected void btnAddAnotherEducation_Click(object sender, EventArgs e)
        {
            ClearEducationFiled();
        }
        #endregion

        #region [Job]
        protected void btnSaveJob_Click(object sender, EventArgs e)
        {
            SaveJobInformation();
            FillMemberJobsList();
        }
        protected void btnRemoveJob_Click(object sender, EventArgs e)
        {
            RemoveJobInformation();
        }
        protected void dlstJobInformation_ItemCommand(object source, DataListCommandEventArgs e)
        {
            ButtonAction(e.CommandName, e.CommandArgument);
        }
        protected void dlstJobInformation_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                CurrentMemberJob = (MemberJobs)e.Item.DataItem;
                if (CurrentMemberJob.IsCurrentJob == true)
                {
                    lbl = (Label)e.Item.FindControl("lblTo");
                    lbl.Text = "CurentlyWorkThere";
                }
            }
        }
        protected void btnAddAnotherJob_Click(object sender, EventArgs e)
        {
            ClearJobFiled();
        }
        #endregion

        #endregion

        #region [ Helper ]
        private void ButtonAction(string btnName, object item)
        {
            if (btnName == "btnRemoveEducation")
            {
                this.Presenter.RemoveEducationInformation(Convert.ToInt32(item));
                FillMemberEducationlist();
            }
            if (btnName == "btnEditEducation")
            {
                EditEducationInformation(Convert.ToInt32(item));
            }
            if (btnName == "btnRemoveJob")
            {
                this.Presenter.RemoveJobInformation(Convert.ToInt32(item));
                FillMemberJobsList();
            }
            if (btnName == "btnEditJob")
            {
                this.EditJobInformation(Convert.ToInt32(item));
            }
        }

        #region [Education]
        private void Fillddlyear()
        {
            for (int i = DateTime.Now.Year; i >= DateTime.Now.Year - 40; i--)
            {
                ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
        private void FillMemberEducationlist()
        {
            dlstEducationInformation.DataSource = this.Presenter.GetCurrentMemberEducations();
            dlstEducationInformation.DataBind();
        }
        private void SaveEducationInformation()
        {
            CurrentMemberEducation = new MemberEducation();
            CurrentMemberEducation.Name = txtCollageUniversity.Text;
            CurrentMemberEducation.Year = Convert.ToInt32(ddlYear.SelectedValue);
            if (rdbCollage.Checked)
                CurrentMemberEducation.Type = 0;
            else
                CurrentMemberEducation.Type = 1;
            if (btnSaveEducation.Attributes["EducationId"] == null)
                this.Presenter.AddNewEducation(CurrentMemberEducation);
            else
            {
                CurrentMemberEducation.EducationId = Convert.ToInt32(btnSaveEducation.Attributes["EducationId"]);
                this.Presenter.UpdateEducationInformation(CurrentMemberEducation);
            }
            ClearEducationFiled();
        }
        private void EditEducationInformation(int Id)
        {
            CurrentMemberEducation = new MemberEducation();
            CurrentMemberEducation = this.Presenter.GetCurrentMemberEducation(Id);
            txtCollageUniversity.Text = CurrentMemberEducation.Name;
            ddlYear.SelectedValue = CurrentMemberEducation.Year.ToString();
            if (CurrentMemberEducation.Type == 0)
                rdbCollage.Checked = true;
            else
                rdbUniversity.Checked = true;
            btnSaveEducation.Attributes.Add("EducationId", CurrentMemberEducation.EducationId.ToString());
            btnRemoveEducation.Attributes.Add("EducationId", CurrentMemberEducation.EducationId.ToString());
        }
        private void RemoveEducationInformation()
        {
            if (btnRemoveEducation.Attributes["EducationId"] != null)
            {
                this.Presenter.RemoveEducationInformation(Convert.ToInt32(btnRemoveEducation.Attributes["EducationId"]));
                FillMemberEducationlist();
            }
            ClearEducationFiled();
        }
        private void ClearEducationFiled()
        {
            txtCollageUniversity.Text = "";
            rdbCollage.Checked = true;
            ddlYear.SelectedValue = DateTime.Now.Year.ToString();
            btnSaveEducation.Attributes.Remove("EducationID");
            btnRemoveEducation.Attributes.Remove("EducationID");
        }

        #endregion
        #region [Job]
        private void FillMemberJobsList()
        {
            dlstJobInformation.DataSource = this.Presenter.GetCurrentMemberJobs();
            dlstJobInformation.DataBind();
        }
        private void SaveJobInformation()
        {
            CurrentMemberJob = new MemberJobs();
            CurrentMemberJob.Employeer = txtEmployeer.Text;
            CurrentMemberJob.Position = txtPosition.Text;
            CurrentMemberJob.EmployedFrom = uxFrom.SelectedDate;
            if (rdbTo.Checked)
            {
                CurrentMemberJob.EmployedTo = uxTo.SelectedDate;
                CurrentMemberJob.IsCurrentJob = false;
            }
            else
                CurrentMemberJob.IsCurrentJob = true;

            if (btnSaveJob.Attributes["JobId"] == null)
                this.Presenter.AddNewJob(CurrentMemberJob);
            else
            {
                CurrentMemberJob.JobId = Convert.ToInt32(btnSaveJob.Attributes["JobId"]);
                this.Presenter.UpdateJobInformation(CurrentMemberJob);
            }
            ClearJobFiled();
        }
        private void EditJobInformation(int Id)
        {
            CurrentMemberJob = new MemberJobs();
            CurrentMemberJob = this.Presenter.GetCurrentMemberJob(Id);
            txtEmployeer.Text = CurrentMemberJob.Employeer;
            txtPosition.Text = CurrentMemberJob.Position;
            uxFrom.SelectedDate = Convert.ToDateTime(CurrentMemberJob.EmployedFrom);
            if (CurrentMemberJob.EmployedTo != null)
            {
                rdbTo.Checked = true;
                uxTo.SelectedDate = Convert.ToDateTime(CurrentMemberJob.EmployedTo);
            }
            else
                rdbCurentlyWorkThere.Checked = true;

            btnSaveJob.Attributes.Add("JobID", CurrentMemberJob.JobId.ToString());
            btnRemoveJob.Attributes.Add("JobID", CurrentMemberJob.JobId.ToString());
        }
        private void RemoveJobInformation()
        {
            if (btnRemoveJob.Attributes["JobId"] != null)
            {
                this.Presenter.RemoveJobInformation(Convert.ToInt32(btnRemoveJob.Attributes["JobId"]));
                FillMemberJobsList();
            }

            ClearJobFiled();
        }
        private void ClearJobFiled()
        {
            txtEmployeer.Text = "";
            txtPosition.Text = "";
            rdbTo.Checked = true;
            uxFrom.SelectedDate = DateTime.Now;
            uxTo.SelectedDate = DateTime.Now;
            btnSaveJob.Attributes.Remove("JobId");
            btnRemoveJob.Attributes.Remove("JobId");
        }

        #endregion

        protected void dlstJobInformation_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        #endregion








    }
}